package com.classroommanagesystem.controller;

import com.classroommanagesystem.common.Result;
import com.classroommanagesystem.domain.dto.LoginDTO;
import com.classroommanagesystem.domain.dto.RegisterDTO;
import com.classroommanagesystem.domain.entity.User;
import com.classroommanagesystem.exception.BusinessException;
import com.classroommanagesystem.service.UserService;
import com.classroommanagesystem.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author victory
 * @date 2025/10/25 18:41
 */

@RestController
@RequestMapping("/public")
@Tag(name = "公共接口")
@Slf4j
public class CommonController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param dto
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result login(@RequestBody LoginDTO dto) {
        log.info("登录请求: {}", dto);
        String token = userService.login(dto);
        if (token != null) return Result.success("登录成功", token);
        else return Result.error("登录失败");
    }

    /**
     * 用户注册
     *
     * @param dto
     * @return
     */
    // @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result register(@RequestBody RegisterDTO dto) {
        log.info("注册请求: {}", dto);
        userService.register(dto);
        return Result.success("注册成功");
    }







}
